package com.cyss.greenball.view;

import java.util.ArrayList;
import java.util.List;

import com.cyss.greenball.main.R;
import com.cyss.greenball.util.SystemUtil;
import com.cyss.greenball.world.Hole;
import com.lianzt.commondata.AbstractCommonData;
import com.lianzt.commondata.DataConvertFactory;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.widget.Toast;

public class CreateMapView extends SurfaceView implements Callback, Runnable {
	private AbstractCommonData acdView;
	private Paint paint;
	private float startX, startY;
	private Canvas canvas;
	private SurfaceHolder holder;
	private Context context;
	private int elementType = SystemUtil.GAME_ELEMENT_LINEBARRIER;
	private AbstractCommonData element;
	private List<AbstractCommonData> elementLineBarrier = new ArrayList<AbstractCommonData>();
	private List<AbstractCommonData> elementRecBarrier = new ArrayList<AbstractCommonData>();
	private List<AbstractCommonData> elementHole = new ArrayList<AbstractCommonData>();
	private List<AbstractCommonData> elementGreenBall = new ArrayList<AbstractCommonData>();
	
	public CreateMapView(Context context) {
		super(context);
		holder = this.getHolder();
		holder.addCallback(this);
		paint = new Paint();
		paint.setColor(Color.RED);
		paint.setStrokeWidth(3);
		this.context = context;
	}

	@Override
	public void run() {

	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		try {
			canvas = holder.lockCanvas();
			canvas.drawColor(Color.GRAY);
			paint.setColor(Color.RED);
			canvas.drawLine(0, 0, 0, SystemUtil.SCREEN_HEIGHT, paint);
			canvas.drawLine(0, 0, SystemUtil.SCREEN_WIDTH, 0, paint);
			canvas.drawLine(SystemUtil.SCREEN_WIDTH, 0,
					SystemUtil.SCREEN_WIDTH, SystemUtil.SCREEN_HEIGHT, paint);
			canvas.drawLine(0, SystemUtil.SCREEN_HEIGHT,
					SystemUtil.SCREEN_WIDTH, SystemUtil.SCREEN_HEIGHT, paint);
			drawList(elementLineBarrier, SystemUtil.GAME_ELEMENT_LINEBARRIER);
			drawList(elementHole, SystemUtil.GAME_ELEMENT_HOLE);
			drawList(elementRecBarrier, SystemUtil.GAME_ELEMENT_RECTANGLEBARRIER);
			drawList(elementGreenBall, SystemUtil.GAME_ELEMENT_GREENBALL);
		} catch (Exception e) {

		} finally {
			try {
				holder.unlockCanvasAndPost(canvas);
			} catch (Exception e) {
			}
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			startX = event.getX();
			startY = event.getY();
			try {
				canvas = holder.lockCanvas();
				canvas.drawColor(Color.GRAY);
				draw(event);
			} catch (Exception e) {

			} finally {
				try {
					holder.unlockCanvasAndPost(canvas);
				} catch (Exception e) {
				}
			}
			break;
		case MotionEvent.ACTION_MOVE:
			try {
				canvas = holder.lockCanvas();
				canvas.drawColor(Color.GRAY);
				draw(event);
			} catch (Exception e) {

			} finally {
				try {
					holder.unlockCanvasAndPost(canvas);
				} catch (Exception e) {
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			element = DataConvertFactory.getInstanceEmpty();
			element.putIntValue("elementType", elementType);
			switch (elementType) {
			case SystemUtil.GAME_ELEMENT_LINEBARRIER:
				element.putFloatValue("startX", startX);
				element.putFloatValue("startY", startY);
				element.putFloatValue("endX", event.getX());
				element.putFloatValue("endY", event.getY());
				elementLineBarrier.add(element);
				break;
			case SystemUtil.GAME_ELEMENT_TRAPHOLE:
				element.putFloatValue("centerX", event.getX());
				element.putFloatValue("centerY", event.getY());
				element.putIntValue("holeType", Hole.TRAP);
				elementHole.add(element);
				break;
			case SystemUtil.GAME_ELEMENT_DESTINYHOLE:
				element.putFloatValue("centerX", event.getX());
				element.putFloatValue("centerY", event.getY());
				element.putIntValue("holeType", Hole.DESTINY);
				elementHole.add(element);
				break;
			case SystemUtil.GAME_ELEMENT_RECTANGLEBARRIER:
				element.putFloatValue("left", startX);
				element.putFloatValue("top", startY);
				element.putFloatValue("right", event.getX());
				element.putFloatValue("bottom", event.getY());
				elementRecBarrier.add(element);
				break;
			case SystemUtil.GAME_ELEMENT_GREENBALL:
				if (elementGreenBall.size() < 1) {
					element.putFloatValue("centerX", event.getX());
					element.putFloatValue("centerY", event.getY());
					elementGreenBall.add(element);
				}
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
		return true;
	}

	private void drawList(List<AbstractCommonData> acdList, int type) {
		switch (type) {
		case SystemUtil.GAME_ELEMENT_LINEBARRIER:
			paint.setStyle(Paint.Style.FILL);
			paint.setColor(Color.RED);
			for (AbstractCommonData acd : acdList) {
				canvas.drawLine(acd.getFloatValue("startX"),
						acd.getFloatValue("startY"), acd.getFloatValue("endX"),
						acd.getFloatValue("endY"), paint);
			}
			break;
		case SystemUtil.GAME_ELEMENT_HOLE:
			paint.setStyle(Paint.Style.STROKE);
			for (AbstractCommonData acd : acdList) {
				if (acd.getIntValue("holeType") == Hole.TRAP) {
					paint.setColor(Color.BLACK);
				} else if (acd.getIntValue("holeType") == Hole.DESTINY) {
					paint.setColor(Color.BLUE);
				} else {
					continue;
				}
				canvas.drawCircle(acd.getFloatValue("centerX"),
						acd.getFloatValue("centerY"),
						SystemUtil.GREEN_BALL_RADIUS, paint);
			}
			break;
		case SystemUtil.GAME_ELEMENT_RECTANGLEBARRIER:
			paint.setStyle(Paint.Style.FILL);
			paint.setColor(Color.RED);
			for (AbstractCommonData acd : acdList) {
				canvas.drawRect(acd.getFloatValue("left"),
						acd.getFloatValue("top"), acd.getFloatValue("right"),
						acd.getFloatValue("bottom"), paint);
			}
			break;
		case SystemUtil.GAME_ELEMENT_GREENBALL:
			paint.setStyle(Paint.Style.FILL);
			paint.setColor(Color.GREEN);
			for (AbstractCommonData acd : acdList) {
				canvas.drawCircle(acd.getFloatValue("centerX"),
						acd.getFloatValue("centerY"),
						SystemUtil.GREEN_BALL_RADIUS, paint);
			}
			break;
		default:
			break;
		}
	}

	public void draw(MotionEvent e) {
		paint.setColor(Color.RED);
		canvas.drawLine(0, 0, 0, SystemUtil.SCREEN_HEIGHT, paint);
		canvas.drawLine(0, 0, SystemUtil.SCREEN_WIDTH, 0, paint);
		canvas.drawLine(SystemUtil.SCREEN_WIDTH, 0, SystemUtil.SCREEN_WIDTH,
				SystemUtil.SCREEN_HEIGHT, paint);
		canvas.drawLine(0, SystemUtil.SCREEN_HEIGHT, SystemUtil.SCREEN_WIDTH,
				SystemUtil.SCREEN_HEIGHT, paint);
		drawList(elementLineBarrier, SystemUtil.GAME_ELEMENT_LINEBARRIER);
		drawList(elementHole, SystemUtil.GAME_ELEMENT_HOLE);
		drawList(elementRecBarrier, SystemUtil.GAME_ELEMENT_RECTANGLEBARRIER);
		drawList(elementGreenBall, SystemUtil.GAME_ELEMENT_GREENBALL);
		switch (elementType) {
		case SystemUtil.GAME_ELEMENT_LINEBARRIER:
			paint.setStyle(Paint.Style.FILL);
			paint.setColor(Color.RED);
			canvas.drawLine(startX, startY, e.getX(), e.getY(), paint);
			break;
		case SystemUtil.GAME_ELEMENT_TRAPHOLE:
			paint.setStyle(Paint.Style.STROKE);
			paint.setColor(Color.BLACK);
			canvas.drawCircle(e.getX(), e.getY(), SystemUtil.GREEN_BALL_RADIUS,
					paint);
			break;
		case SystemUtil.GAME_ELEMENT_DESTINYHOLE:
			paint.setStyle(Paint.Style.STROKE);
			paint.setColor(Color.BLUE);
			canvas.drawCircle(e.getX(), e.getY(), SystemUtil.GREEN_BALL_RADIUS,
					paint);
			break;
		case SystemUtil.GAME_ELEMENT_RECTANGLEBARRIER:
			paint.setColor(Color.RED);
			canvas.drawRect(startX, startY, e.getX(), e.getY(), paint);
			break;
		case SystemUtil.GAME_ELEMENT_GREENBALL:
			if (elementGreenBall.size() < 1) {
				paint.setStyle(Paint.Style.FILL);
				paint.setColor(Color.GREEN);
				canvas.drawCircle(e.getX(), e.getY(),
						SystemUtil.GREEN_BALL_RADIUS, paint);
			} else {
				Toast.makeText(context,
						context.getString(R.string.must_one_green_ball),
						Toast.LENGTH_LONG).show();
			}
			break;
		default:
			break;
		}
	}

	public int getElementType() {
		return elementType;
	}

	public void setElementType(int elementType) {
		this.elementType = elementType;
	}

	public AbstractCommonData getSaveData() {
		acdView = DataConvertFactory.getInstance();
		AbstractCommonData head = acdView.getDataValue("head");
		head.putIntValue("screenHeight", SystemUtil.SCREEN_HEIGHT);
		head.putIntValue("screenWidth", SystemUtil.SCREEN_WIDTH);
		acdView.putArrayValue("lineBarrier", elementLineBarrier);
		acdView.putArrayValue("recBarrier", elementRecBarrier);
		acdView.putArrayValue("hole", elementHole);
		acdView.putArrayValue("greenBall", elementGreenBall);
		return acdView;
	}

	public void removeElementRecently() {
		canvas = holder.lockCanvas();
		canvas.drawColor(Color.GRAY);
		paint.setColor(Color.RED);
		canvas.drawLine(0, 0, 0, SystemUtil.SCREEN_HEIGHT, paint);
		canvas.drawLine(0, 0, SystemUtil.SCREEN_WIDTH, 0, paint);
		canvas.drawLine(SystemUtil.SCREEN_WIDTH, 0, SystemUtil.SCREEN_WIDTH,
				SystemUtil.SCREEN_HEIGHT, paint);
		canvas.drawLine(0, SystemUtil.SCREEN_HEIGHT, SystemUtil.SCREEN_WIDTH,
				SystemUtil.SCREEN_HEIGHT, paint);
		switch (elementType) {
		case SystemUtil.GAME_ELEMENT_LINEBARRIER:
			elementLineBarrier.remove(element);
			break;
		case SystemUtil.GAME_ELEMENT_HOLE:
			elementHole.remove(element);
			break;
		case SystemUtil.GAME_ELEMENT_RECTANGLEBARRIER:
			elementRecBarrier.remove(element);
			break;
		case SystemUtil.GAME_ELEMENT_GREENBALL:
			elementGreenBall.remove(element);
			break;
		default:
			break;
		}
		drawList(elementLineBarrier, SystemUtil.GAME_ELEMENT_LINEBARRIER);
		drawList(elementHole, SystemUtil.GAME_ELEMENT_HOLE);
		drawList(elementRecBarrier, SystemUtil.GAME_ELEMENT_RECTANGLEBARRIER);
		drawList(elementGreenBall, SystemUtil.GAME_ELEMENT_GREENBALL);
		holder.unlockCanvasAndPost(canvas);
	}

}
